package org.xdq.可重入锁;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test02 {

    public static void main(String[] args) {

        Lock lock =
                new ReentrantLock();//创建一个可重入非公平锁

        //new ReentrantLock(true)//创建一个可重入公平锁

        new Thread(()->{
            lock.lock();//加锁
            try {
                for(int i=0;i<100;i++){
                    System.out.print(i+" ");
                }
            } finally {
                lock.unlock();//释放锁
            }


        }).start();


        new Thread(()->{
            lock.lock();//加锁
            try {
                for(int i=100;i<200;i++){
                    System.out.print(i+ " ");
                }
            } finally {
                lock.unlock();//释放锁
            }


        }).start();
    }
}
